package com.atwj.controller;

import com.atwj.Service.BookService;
import com.atwj.Service.impl.BookServiceImpl;
import com.atwj.entity.Book;
import com.atwj.entity.Page;
import com.atwj.utils.webUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author 吴先森
 * @description:
 * @create 2022-03-23 19:56
 */
public class BookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();


    //处理图书管理页面中数据的分页操作
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数 pageNo和pageSize
        int pageNo = webUtil.parseInt(req.getParameter("pageNo"), 1);
        int pageSize = webUtil.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
        //调用BookService.page(pageNo, pageSize) Page对象
        Page<Book> page = bookService.page(pageNo, pageSize);

        page.setUrl("manager/bookServlet?action=page");

        //保存page对象到request域
        req.setAttribute("page", page);
        //请求转发到pages/manager/boo_edit页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNo = webUtil.parseInt(req.getParameter("pageNo"), 0);
        pageNo += 1;
        //获取请求的参数==封装成为Book对象
        Book book = webUtil.copyParamToBean(req.getParameterMap(), new Book());
        //调用BookService.addBook()方法
        bookService.addBook(book);
        //跳到图书列表界面
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数id
        int id = webUtil.parseInt(req.getParameter("id"), 0);
        //调用ookService.deleteBookByid()
        bookService.deleteBook(id);
        //重定向回图书管理页面
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        Book book = webUtil.copyParamToBean(req.getParameterMap(), new Book());
        //调用BookService.updateBook（）修改图书
        bookService.update(book);
        //重定向图书管理页面
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求的参数图书编号
        int id = webUtil.parseInt(req.getParameter("id"), 0);
        //调用bookService.queryBookById（）方法查询图书
        Book book = bookService.queryBookByid(id);
        //保存图书信息到Request域中
        req.setAttribute("book", book);
        //请求转发到pages/manager/book_edit.jsp
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req, resp);
    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过BookService查询全部图书
        List<Book> books = bookService.queryBooks();
        //把全部图书保存到Request域中
        req.setAttribute("books", books);
        //请求转发到/pages/manager/book_manager.jsp中
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }
}
